﻿using ElectronicShoppingMall.Service.Models.Enterprise;
using ElectronicShoppingMall.Service.Services.Enterprise;
using ElectronicShoppingMall.Web.Back.Filter;
using ElectronicShoppingMall.Web.Back.DTOS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ElectronicShoppingMall.Web.Back.Controllers
{
    public class OrderController : ApiController
    {
        private OrderService _serviceOrder;
        public OrderController()
        {
            _serviceOrder = new OrderService();
        }
        //获取未发货的订单
        [HttpGet]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.Deliver)]
        public List<OrderModel> GetAllOrdersNotShipped()
        {
            try
            {
                return _serviceOrder.GetAllOrdersNotShipped();
            }
            catch
            {
                return null;
            }
        }
        //商品发货
        [Authorize]
        [HttpPut]
        [EmployeePermission(PermissionCode = Permissions.Deliver)]
        public List<OrderModel> DeliverGoods([FromBody]string orderId)
        {
            try
            {
                _serviceOrder.DeliverGoods(orderId);
                return GetAllOrdersNotShipped();
            }
            catch
            {
                return null;
            }
        }
        //获取所有未确认发货的订单
        [Authorize]
        [HttpGet]
        [EmployeePermission(PermissionCode = Permissions.ConfirmationOfDelivery)]
        public List<OrderModel> GetUnconfirmedDeliveryOrder()
        {
            try
            {
              return   _serviceOrder.GetUnconfirmedDeliveryOrder();
            }
            catch
            {
                return null;
            }

        }
        //商品确认发货
        [Authorize]
        [HttpPut]
        [EmployeePermission(PermissionCode = Permissions.ConfirmationOfDelivery)]
        public List<OrderModel> ConfirmationOfDelivery([FromBody]string orderId)
        {
            try
            {
                _serviceOrder.ConfirmationOfDelivery(orderId);
                return GetUnconfirmedDeliveryOrder();
            }
            catch
            {
                return null;
            }
        }

        //获取所有申请退货的订单
        [Authorize]
        [HttpGet]
        [EmployeePermission(PermissionCode = Permissions.RenturnGoods)]
        [OperationLog(OperatDetail =Operations.ViewReturns)]
        public List<OrderModel> GetReturnOrder()
        {
            try
            {
                return _serviceOrder.GetReturnOrder();
            }
            catch
            {
                return null;
            }

        }

        //处理申请退货的订单
        [Authorize]
        [HttpPut]
        [EmployeePermission(PermissionCode = Permissions.RenturnGoods)]
        [OperationLog(OperatDetail = Operations.RefundProcessing)]
        public List<OrderModel> ProcessingOrdersForReturn(dynamic order)
        {
            try
            {
                _serviceOrder.ProcessingOrdersForReturn(Convert.ToString(order.orderId), Convert.ToString(order.operation));
                return GetReturnOrder();
            }
            catch
            {
                return null;
            }
        }

        //获取所有申请退款的订单
        [Authorize]
        [HttpGet]
        [EmployeePermission(PermissionCode = Permissions.Refund)]
        public List<OrderModel> GetRefundOrders()
        {
            try
            {
                return _serviceOrder.GetRefundOrders();
            }
            catch
            {
                return null;
            }
        }
        //处理申请退款的订单
        [Authorize]
        [HttpPut]
        [EmployeePermission(PermissionCode = Permissions.Refund)]
        public List<OrderModel> ProcessingOrdersForRefund(dynamic order)
        {
            try
            {
                _serviceOrder.ProcessingOrdersForRefund(Convert.ToString(order.orderId), Convert.ToString(order.operation));
                return GetRefundOrders();
            }
            catch
            {
                return null;
            }
        }
    }
}
